replace the last uses of QRegExp. (#743)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Mon, 25 Oct 2021 00:15:15 +0000 (18:15 -0600)
committerGitHub <noreply@github.com>
Mon, 25 Oct 2021 00:15:15 +0000 (18:15 -0600)
also, correct the discard documentation.  The given example didn't
work in recent releases.

discard.cc
discard.h
trackfilter.cc
trackfilter.h
xmldoc/filters/options/discard-matchname.xml

index a052e3663225ad1638a85923dcdece07acb28bb4..d0c1e8ca1a38971ed3b348982231f7ea43115fff 100644 (file)
 
  */
 
-#include "defs.h"
 #include "discard.h"
-#include <cstdlib>
-// Can't use QRegularExpression because Linux won't get Qt 5 for years.
-#include <QRegExp>
-#include <cstdio>
-#include <cstdlib>
+
+#include <QDebug>              // for QDebug
+#include <QRegularExpression>  // for QRegularExpression, QRegularExpression::CaseInsensitiveOption, QRegularExpressionMatch
+
+#include <cstdlib>             // for atoi, atof
+
+#include "defs.h"              // for Waypoint, fatal, route_del_wpt, route_disp_all, track_del_wpt, track_disp_all, waypt_del, waypt_disp_all, route_head, rtedata, trkdata, wptdata, fix_none, fix_unknown
+#include "src/core/logging.h"  // for FatalMsg
+
 
 #if FILTERS_ENABLED
 
@@ -73,16 +76,16 @@ void DiscardFilter::fix_process_wpt(const Waypoint* wpt)
     del = 1;
   }
 
-  if (nameopt && name_regex.indexIn(waypointp->shortname) >= 0) {
+  if (nameopt && name_regex.match(waypointp->shortname).hasMatch()) {
     del = 1;
   }
-  if (descopt && desc_regex.indexIn(waypointp->description) >= 0) {
+  if (descopt && desc_regex.match(waypointp->description).hasMatch()) {
     del = 1;
   }
-  if (cmtopt && cmt_regex.indexIn(waypointp->notes) >= 0) {
+  if (cmtopt && cmt_regex.match(waypointp->notes).hasMatch()) {
     del = 1;
   }
-  if (iconopt && icon_regex.indexIn(waypointp->icon_descr) >= 0) {
+  if (iconopt && icon_regex.match(waypointp->icon_descr).hasMatch()) {
     del = 1;
   }
 
@@ -130,6 +133,19 @@ void DiscardFilter::process()
 
 }
 
+QRegularExpression DiscardFilter::generateRegExp(const QString& glob_pattern)
+{
+  QRegularExpression regex;
+  regex.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
+  QString pattern = QRegularExpression::wildcardToRegularExpression(glob_pattern);
+  // un-anchor the pattern, we allow partial matches.
+  if (pattern.startsWith("\\A") && pattern.endsWith("\\z")) {
+    pattern = pattern.mid(2,pattern.size()-4);
+  }
+  regex.setPattern(pattern);
+  return regex;
+}
+
 void DiscardFilter::init()
 {
   if (hdopopt) {
@@ -159,24 +175,28 @@ void DiscardFilter::init()
   }
 
   if (nameopt) {
-    name_regex.setCaseSensitivity(Qt::CaseInsensitive);
-    name_regex.setPatternSyntax(QRegExp::WildcardUnix);
-    name_regex.setPattern(nameopt);
+    name_regex = generateRegExp(nameopt);
+    if (!name_regex.isValid()) {
+      fatal(FatalMsg() << "discard: matchname option is an invalid expression.");
+    }
   }
   if (descopt) {
-    desc_regex.setCaseSensitivity(Qt::CaseInsensitive);
-    desc_regex.setPatternSyntax(QRegExp::WildcardUnix);
-    desc_regex.setPattern(descopt);
+    desc_regex = generateRegExp(descopt);
+    if (!desc_regex.isValid()) {
+      fatal(FatalMsg() << "discard: matchdesc option is an invalid expression.");
+    }
   }
   if (cmtopt) {
-    cmt_regex.setCaseSensitivity(Qt::CaseInsensitive);
-    cmt_regex.setPatternSyntax(QRegExp::WildcardUnix);
-    cmt_regex.setPattern(cmtopt);
+    cmt_regex = generateRegExp(cmtopt);
+    if (!cmt_regex.isValid()) {
+      fatal(FatalMsg() << "discard: matchcmt option is an invalid expression.");
+    }
   }
   if (iconopt) {
-    icon_regex.setCaseSensitivity(Qt::CaseInsensitive);
-    icon_regex.setPatternSyntax(QRegExp::WildcardUnix);
-    icon_regex.setPattern(iconopt);
+    icon_regex = generateRegExp(iconopt);
+    if (!icon_regex.isValid()) {
+      fatal(FatalMsg() << "discard: matchicon option is an invalid expression.");
+    }
   }
 }
 
index 1dbb234f8af695b1df2fde378392fe2d29958834..e27b41c51a10fffc644732b973204a1c9b96c84b 100644 (file)
--- a/discard.h
+++ b/discard.h
 #ifndef DISCARD_H_INCLUDED_
 #define DISCARD_H_INCLUDED_
 
-// Can't use QRegularExpression because Linux won't get Qt 5 for years.
-#include <QRegExp>         // for QRegExp
-#include <QVector>         // for QVector
+#include <QRegularExpression>  // for QRegularExpression
+#include <QString>             // for QString
+#include <QVector>             // for QVector
 
-#include "defs.h"          // for ARG_NOMINMAX, ARGTYPE_BEGIN_REQ, ARGTYPE_S...
-#include "filter.h"        // for Filter
+#include "defs.h"              // for arglist_t, ARG_NOMINMAX, ARGTYPE_BEGIN_REQ, ARGTYPE_STRING, ARGTYPE_BOOL, ARGTYPE_INT, ARGTYPE_FLOAT, route_head, ARGTYPE_END_REQ, Waypoint, gpsdata_type
+#include "filter.h"            // for Filter
 
 #if FILTERS_ENABLED
 class DiscardFilter:public Filter
@@ -40,6 +40,9 @@ public:
   void init() override;
   void process() override;
 
+private:
+  QRegularExpression generateRegExp(const QString& glob_pattern);
+
 private:
   char* hdopopt = nullptr;
   char* vdopopt = nullptr;
@@ -50,13 +53,13 @@ private:
   char* eleminopt = nullptr;
   char* elemaxopt = nullptr;
   char* nameopt = nullptr;
-  QRegExp name_regex;
+  QRegularExpression name_regex;
   char* descopt = nullptr;
-  QRegExp desc_regex;
+  QRegularExpression desc_regex;
   char* cmtopt = nullptr;
-  QRegExp cmt_regex;
+  QRegularExpression cmt_regex;
   char* iconopt = nullptr;
-  QRegExp icon_regex;
+  QRegularExpression icon_regex;
 
   double hdopf{};
   double vdopf{};
index 6d2ab94af52c7e7431aa8f28494aa9928b36731e..079bf36e7c71dfcebf458ac23fb6108b0f953301 100644 (file)
 #include <QChar>                           // for QChar
 #include <QDate>                           // for QDate
 #include <QDateTime>                       // for QDateTime
-#ifdef TRACKF_DBG
 #include <QDebug>
-#endif
 #include <QList>                           // for QList<>::iterator, QList, QList<>::const_iterator
-#include <QRegExp>                         // for QRegExp, QRegExp::WildcardUnix
 #include <QRegularExpression>              // for QRegularExpression, QRegularExpression::CaseInsensitiveOption, QRegularExpression::PatternOptions
 #include <QRegularExpressionMatch>         // for QRegularExpressionMatch
 #include <QString>                         // for QString
@@ -52,6 +49,7 @@
 
 #include "grtcirc.h"                       // for RAD, gcdist, radtometers, heading_true_degrees
 #include "src/core/datetime.h"             // for DateTime
+#include "src/core/logging.h"              // for FatalMsg
 
 
 #if FILTERS_ENABLED || MINIMAL_FILTERS
@@ -187,7 +185,12 @@ void TrackFilter::trackfilter_fill_track_list_cb(const route_head* track)  /* ca
   }
 
   if (opt_name != nullptr) {
-    if (!QRegExp(opt_name, Qt::CaseInsensitive, QRegExp::WildcardUnix).exactMatch(track->rte_name)) {
+    QRegularExpression regex(QRegularExpression::wildcardToRegularExpression(opt_name),
+                             QRegularExpression::CaseInsensitiveOption);
+    if (!regex.isValid()) {
+      fatal(FatalMsg() << "track: name option is an invalid expression.");
+    }
+    if (!regex.match(track->rte_name).hasMatch()) {
       foreach (Waypoint* wpt, track->waypoint_list) {
         track_del_wpt(const_cast<route_head*>(track), wpt);
         delete wpt;
index 721f02d9fd7df304699630a0b7da92c8b3e97a24..79eccb1a37a2f9ed70ba567cd9002339471586f4 100644 (file)
 #ifndef TRACKFILTER_H_INCLUDED_
 #define TRACKFILTER_H_INCLUDED_
 
-#include <QDateTime>         // for QDateTime
-#include <QList>             // for QList
-#include <QVector>           // for QVector
-#include <QtGlobal>          // for qint64
-
-#include "defs.h"            // for ARG_NOMINMAX, route_head (ptr only), ARG...
-#include "filter.h"          // for Filter
+#include <QDateTime>            // for QDateTime
+#include <QList>                // for QList
+#include <QVector>              // for QVector
+#include <QtGlobal>             // for qint64
+
+#include "defs.h"               // for ARG_NOMINMAX, route_head (ptr only), ARG...
+#include "filter.h"             // for Filter
+#include "src/core/datetime.h"  // for DateTime
 
 #if FILTERS_ENABLED || MINIMAL_FILTERS
 
index 1162329c09728cc500ce47395715717d56b8115e..5073cdf0caa148d88a608e2e129c0b806b1737a8 100644 (file)
@@ -10,7 +10,7 @@ by an alphanumeric sequence of variable length.  To remove all six character
 long IDs that between (and including) GC1000 and GC2FFF, you could use
 </para>
 <para><userinput>
-gpsbabel -i geo -f geocaching.loc -x discard,matchname=GC[1-2]...
+gpsbabel -i geo -f geocaching.loc -x discard,matchname=GC[1-2]???
 </userinput>
 to discard all GCs followed by exactly three characters.
 </para>